package view;

import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import contacts614.po.Student;
import controller.StudentControllerListener;

public class StudentDialog extends JDialog {
	// 要依赖mainFrame（对话框）
	private StudentControllerListener studentControllerListener;
	private JTextField studentNoTextField, studentNameTextField, roomNoTextField, studentBirthTextField,
			studentTelephoneTextField, studentEmailTextField, studentAddressTextField;
	private JButton confirmButton;
	private JPanel studentAddPanel;
	private Student student;

	/**
	 * 学生对话框
	 * 
	 * @param owner
	 * @param title
	 * @param student
	 */
	public StudentDialog(StudentFrame owner, String title, Student student) {
		this.studentControllerListener = owner.getStudentControllerListener();
		this.studentControllerListener.setStudentDialog(this);// 让StudentFrame和StudentDialog共用一个controller对象
		this.setTitle(title);
		this.student = student;
		this.add(getStudentAddPanel());
		this.setModal(true);
		this.setSize(800, 800);// 对话框大小
		this.setLocationRelativeTo(null);// 设定窗口相对于组件的位置
		this.setVisible(true);
		this.validate();
	}

	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	private JPanel getStudentAddPanel() {
		studentAddPanel = new JPanel();
		if ("添加学生".equals(this.getTitle())) {
			studentNoTextField = new JTextField(12);
			studentNameTextField = new JTextField(12);
			roomNoTextField = new JTextField(12);
			studentBirthTextField = new JTextField(12);
			studentTelephoneTextField = new JTextField(12);
			studentEmailTextField = new JTextField(12);
			studentAddressTextField = new JTextField(12);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addPost");
		} else {
			studentNoTextField = new JTextField(student.getStudentNo(), 12);
			studentNameTextField = new JTextField(student.getStudentName(), 12);
			roomNoTextField = new JTextField(String.valueOf(student.getRoom()), 12);
			studentBirthTextField = new JTextField(String.valueOf(student.getStudentBirth()), 12);
			studentTelephoneTextField = new JTextField(student.getStudentTelephone(), 12);
			studentEmailTextField = new JTextField(student.getStudentEmail(), 12);

			studentAddressTextField = new JTextField(student.getStudentAddress(), 12);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updatePost");// 修改图书

		}
		confirmButton.addActionListener(studentControllerListener);
		studentAddPanel.add(new JLabel("学号"));// 标签，提示信息
		studentAddPanel.add(studentNoTextField);
		studentAddPanel.add(new JLabel("学生姓名"));
		studentAddPanel.add(studentNameTextField);
		studentAddPanel.add(new JLabel("宿舍号"));
		studentAddPanel.add(roomNoTextField);
		studentAddPanel.add(new JLabel("出生日期"));
		studentAddPanel.add(studentBirthTextField);
		studentAddPanel.add(new JLabel("电话号码"));
		studentAddPanel.add(studentTelephoneTextField);
		studentAddPanel.add(new JLabel("邮箱"));
		studentAddPanel.add(studentEmailTextField);
		studentAddPanel.add(new JLabel("家庭地址"));
		studentAddPanel.add(studentAddressTextField);
		studentAddPanel.add(confirmButton);
		return studentAddPanel;
	}

	public StudentControllerListener getStudentControllerListener() {
		return studentControllerListener;
	}

	public JTextField getStudentNoTextField() {
		return studentNoTextField;
	}

	public JTextField getStudentNameTextField() {
		return studentNameTextField;
	}

	public JTextField getRoomNoTextField() {
		return roomNoTextField;
	}

	public JTextField getStudentBirthTextField() {
		return studentBirthTextField;
	}

	public JTextField getStudentTelephoneTextField() {
		return studentTelephoneTextField;
	}

	public JTextField getStudentEmailTextField() {
		return studentEmailTextField;
	}

	public JTextField getStudentAddressTextField() {
		return studentAddressTextField;
	}

	public JButton getConfirmButton() {
		return confirmButton;
	}

	public Student getStudent() {
		return student;
	}

}
